//
//  ViewController.m
//  RAC实现登录小demo
//
//  Created by 梁森 on 2018/3/26.
//  Copyright © 2018年 梁森. All rights reserved.
//

#import "ViewController.h"

#import "LSLoginView.h"

#import "LSLoginViewModel.h"
#import <ReactiveObjC.h>
@interface ViewController ()

@property (nonatomic, strong) LSLoginViewModel * viewModel;
@property (nonatomic, strong) LSLoginView * loginView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor greenColor];
    LSLoginView * loginView = [[LSLoginView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:loginView];
    self.loginView = loginView;
    
    [self loginBtnEnable];
    [self loginBtnAction];
}
#pragma mark --- 登录按钮是否可点击
- (void)loginBtnEnable{
    // 将改变view的显示状态放到了view model层中  （数据的绑定）
    RAC(self.viewModel, username) = self.loginView.username.rac_textSignal;
    RAC(self.viewModel, password) = self.loginView.password.rac_textSignal;
    
    RAC(self.loginView.loginBtn, enabled) = self.viewModel.loginEnable;
}
#pragma mark --- 登录按钮点击事件
- (void)loginBtnAction{
    // 登录按钮进行的操作放到了viewmodel层中
    [[self.loginView.loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.viewModel.loginCommand execute:nil];
    }];
}

- (LSLoginViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[LSLoginViewModel alloc] init];
    }
    return _viewModel;
}

@end
